<?php

/**
 * 
 */
require_once '../ConnectFour.php';
require_once '../FourInALinePlayer.php';
require_once '../Player.php';

class MoveCheckerTest extends PHPUnit_Framework_TestCase {

    public function testMoveChecker() {
        $grid = array(
            array(   1,    1,    1, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $checker = new \DankerBot\MoveChecker();
        $player = new \DankerBotPlayer(FourInALinePlayer::PLAYER_ONE, 5, 5);
        $board = new \DankerBot\Board($grid, $player, $checker);
        $checker->setBoard($board);

        echo $board->getDisplayGrid();

        $this->assertTrue($checker instanceof \DankerBot\MoveChecker);

        $this->assertEquals($checker->isOneMoveToWin(), 0);

        $grid = array(
            
            array(null, null, null, null, null),
            array(   2,    1,    1,    1, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneMoveToWin(), 1);

        $grid = array(

            array(null, null, null, null, null),
            array(   2,    1,    1,    1, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneMoveToWin(), 1);

         $grid = array(

            array(   1, null, null, null, null),
            array(   1,    1,    2,    1, null),
            array(null, null, null, null, null),
            array(   1, null, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneMoveToWin(), 2);
        
    
        $grid = array(
            
            array(   1,    2,    1,    1,    1),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneMoveToWin(), 0);

        $grid = array(

            array(   2, null, null, null, null),
            array(   1,    2,    1,    1, null),
            array(   1,    1, null, null, null),
            array(   1,    2,    1, null, null),
            array(   2,    2,    2, null, null));

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneMoveToWin(), 4);


        $grid = array(

            array(   2, null, null, null, null),
            array(   1,    2,    1,    1, null),
            array(   1,    1, null, null, null),
            array(   1,    2,    1, null, null),
            array(   2, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneMoveToWin(), 4);

        $grid = array(

            array(   2,    2,    1,    1,    1),
            array(   1,    2,    1,    1, null),
            array(   1,    1,    1, null, null),
            array(   1,    2,    2, null, null),
            array(   2, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneMoveToWin(), 0);

        $grid = array(

            array(   2,    2,    1,    1,    1),
            array(   2,    2,    1,    1, null),
            array(   1,    1,    1,    2, null),
            array(   1,    2,    2, null, null),
            array(   1, null, null, null, null));

        $board->setGrid($grid);
        $rez = $checker->isOneMoveToWin();
        $this->assertEquals($rez, false);
        
        
        $grid = array(

            array(   2,    2,    1,    2,    1),
            array(   2,    2,    1,    1, null),
            array(   1,    1,    2,    1,    1),
            array(   1,    2,    1, null, null),
            array(   1, null, null, null, null));

        $board->setGrid($grid);
        $rez = $checker->isOneMoveToWin();
        $this->assertEquals($rez, false);

        $grid = array(

            array(   2,    2,    1, null, null),
            array(   2,    2,    1,    1, null),
            array(   1,    1,    2,    1,    1),
            array(   1,    2,    1,    2, null),
            array(   1, null, null, null, null));

        $board->setGrid($grid);
        $rez = $checker->isOneMoveToWin();
        echo "\n\n stop \n\n";
        echo $board->getDisplayGrid();
        $this->assertEquals($rez, 0);


        $grid = array(

            array(null, null, null, null, null),
            array(   2,    1,    2,    1, null),
            array(   2,    2,    1, null, null),
            array(   2,    1, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        echo $board->getDisplayGrid();
        $this->assertEquals($checker->isOneMoveToWin(), 4);

    }

    public function testEnemyOneMoveToWinSecond(){
        /**
         *   . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . . . . . . . .
    . . . X . . X . . .
    . X O O O O X . . .
         */
        $grid = array(
            array(null, null, null, null, null, null, null, null, null, null),
            array(   1, null, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(   2,    1, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(   1,    1, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            );

        $checker = new \DankerBot\MoveChecker();
        $player = new \DankerBotPlayer(FourInALinePlayer::PLAYER_ONE, 10, 10);
        $board = new \DankerBot\Board($grid, $player, $checker);
        $checker->setBoard($board);
        $this->assertEquals($checker->isOneEnemyMoveToWin(),  4);

        $grid = array(
            array(null, null, null, null, null, null, null, null, null, null),
            array(   1, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(   2,    1, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(   1,    1, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            );

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneEnemyMoveToWin(),  2);

        $grid = array(
            array(null, null, null, null, null, null, null, null, null, null),
            array(   1, null, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(   2,    1, null, null, null, null, null, null, null, null),
            array(   2, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(   1,    1, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            array(null, null, null, null, null, null, null, null, null, null),
            );

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneEnemyMoveToWin(),  5);
        
    }

    public function testEnemyOneMoveToWin(){
        $grid = array(
            array(   1,    1,    1, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $checker = new \DankerBot\MoveChecker();
        $player = new \DankerBotPlayer(FourInALinePlayer::PLAYER_ONE, 5, 5);
        $board = new \DankerBot\Board($grid, $player, $checker);
        $checker->setBoard($board);

        echo $board->getDisplayGrid();

        $this->assertTrue($checker instanceof \DankerBot\MoveChecker);

        $this->assertEquals($checker->isOneEnemyMoveToWin(), false);

        $grid = array(

            array(   2, null, null, null, null),
            array(   1,    2,    1,    1, null),
            array(   1,    1,    2, null, null),
            array(   1,    2,    1, null, null),
            array(   2, null, null, null, null));

        $board->setGrid($grid);
        $this->assertEquals($checker->isOneEnemyMoveToWin(), 3);

        $grid = array(

            array(   2, null, null, null, null),
            array(   1,    2,    1,    1, null),
            array(   1,    1,    2,    2,    2),
            array(   1,    2,    1,    1, null),
            array(   2, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneEnemyMoveToWin(), 2);
        $this->assertTrue($checker->isOneEnemyMoveToWin() == false);

        $grid = array(

            array(   2,    1,    2,    2,    2),
            array(   1,    2,    1,    1, null),
            array(   1,    1,    2,    2,    2),
            array(   1,    2,    1, null, null),
            array(   2, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneEnemyMoveToWin(), 0);
        $this->assertEquals($checker->isOneEnemyMoveToWin(), 3);


        $grid = array(
            array(   1,    1,    1,    2, null),
            array(null, null, null, null, null),
            array(   1,    1,    2,    2,    2),
            array(null, null, null, null, null),
            array(null, null, null, null, null));

        $board->setGrid($grid);
        $this->assertNotEquals($checker->isOneEnemyMoveToWin(), 0);
        $this->assertNotEquals($checker->isOneEnemyMoveToWin(), 2);


    }

}
